package com.linh.trade.valuation;

import java.util.ArrayList;
import java.util.List;

import com.linh.trade.Trade;
import com.linh.trade.Trade.Direction;
import com.linh.trade.Trade.State;
import com.linh.trade.data.Point;

public class Valuator {

	private List<Trade> currentTrades;

	public Valuator() {
		super();
		currentTrades = new ArrayList<Trade>();
	}
	
	public void clear(){
		currentTrades.clear();
	}

	public void addTrade(Trade trade) {
		currentTrades.add(trade);
	}

	public double realizedPnL(Trade trade) {
		return 10
				* trade.getAmount()
				* (trade.getClosingPoint().getRate() - trade.getOpeningPoint()
						.getRate())
				* (trade.getDirection() == Direction.LONG ? 1 : -1);
	}

	public double unrealizedPnL(Trade trade, Point evaluationPoint) {
		return 10
				* trade.getAmount()
				* (evaluationPoint.getRate() - trade.getOpeningPoint()
						.getRate())
				* (trade.getDirection() == Direction.LONG ? 1 : -1);
	}

	public double realizedPnL() {
		double pnl = 0;
		for (Trade trade : currentTrades) {
			if (trade.getState() == State.CLOSE) {
				pnl += realizedPnL(trade);
			}
		}
		return pnl;
	}

	public double pnlAt(Point evaluationPoint) {
		double pnl = 0;
		for (Trade trade : currentTrades) {
			if (trade.getState() == State.CLOSE) {
				pnl += realizedPnL(trade);
			} else {
				pnl += unrealizedPnL(trade, evaluationPoint);
			}
		}
		return pnl;
	}

	public List<Trade> getCurrentTrades() {
		return currentTrades;
	}

}
